package cn.mdmm.controller;

import java.io.PrintWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.mdmm.service.user.WxUserDetailedInfoService;
import cn.mdmm.service.weixin.WeiXinServer;

/**
 * 类描述： 
 * 创建人：邓强   
 * 创建时间：2016年11月21日 上午8:50:20     
 * 修改人：  
 * 修改时间：
 * 修改备注：     
 * @version   V1.0      
 */
@Controller
@RequestMapping("wx")
public class WeiXinController {
	Logger logger = LoggerFactory.getLogger(WeiXinController.class);
	@Autowired
	WxUserDetailedInfoService wxUserDetailedInfoService;
	@Autowired
	WeiXinServer WeiXinServer;

	
	@RequestMapping(value="",method = {RequestMethod.GET})
	public void check(String signature, String timestamp, String nonce, String echostr, PrintWriter out){
		if(WeiXinServer.validateWx(signature, timestamp, nonce)){
			out.print(echostr);
		}else{
			out.print("fail");
		}
	}
	
	@RequestMapping(value="",method = {RequestMethod.POST})
	public void handleMsg(String signature, String timestamp, String nonce,@RequestBody String xml,PrintWriter out){
		logger.info(xml);
		if(WeiXinServer.validateWx(signature, timestamp, nonce)){
			String renDate = WeiXinServer.handleMsg(xml);
			out.print(renDate);
		}else{
			out.print("fail");
		}
	}
	
	@RequestMapping(value="pay")
	public void pay(@RequestBody String xml,PrintWriter out){
		logger.info(xml);
		String renDate = WeiXinServer.wxOrder(xml);
		out.print(renDate);
	}
}
